package views;

import java.util.Hashtable;
import java.util.List;

import views.DockStarred.IconicAdapter;

import com.example.android.google.apis.R;
import com.example.android.google.apis.com.example.android.apis.MapsDemo;

import docks.Dock;
import docks.DockController;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DockList extends ListActivity {

    public void onCreate(Bundle icicle) {

	super.onCreate(icicle);
	setContentView(R.layout.list);
	IconicAdapter ia = new IconicAdapter(getItems());
	setListAdapter(ia);
	ListController.setDockList(this);
//	ListView v = ((ListView) findViewById(R.layout.list));
	
	getListView().setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		    long arg3) {
		
		Dock d = (Dock) arg0.getItemAtPosition(arg2);
		Intent intent = new Intent(DockList.this, MapsDemo.class);
		intent.putExtra("moveToDock", true);
		intent.putExtra("latitude", d.getLatitude());
		intent.putExtra("longitude", d.getLongitude());
		startActivity(intent);
	    }
	    
	    
	});

    }

    protected Dock[] getItems() {

	List<Dock> docks = DockController.getInstance().getAllDocks();
	Dock[] ad = new Dock[docks.size()];
	return docks.toArray(ad);
    }
    
    public void setListAdapter() {

	setListAdapter(new IconicAdapter(getItems()));
    }

    class IconicAdapter extends ListController {

//	Dock[] items;

	IconicAdapter(Dock[] docks) {

	    super(DockList.this, R.layout.row, docks);
//	    this.items = docks;
	    ListController.setAllAdapter(this);
	}
	

	public View getView(final int position, View convertView,
		ViewGroup parent) {

	    LayoutInflater inflater = getLayoutInflater();
	    View row = inflater.inflate(R.layout.row, parent, false);
	    TextView label = (TextView) row.findViewById(R.id.labelname);
	    TextView free = (TextView) row.findViewById(R.id.labelfree);
	    TextView available = (TextView) row
		    .findViewById(R.id.labelavailable);

	    final ImageView ib = (ImageView) row
		    .findViewById(R.id.imageButton1);

	    if (items[position].isStarred()) {

		ib.setImageDrawable(getResources().getDrawable(
			R.drawable.starred));
	    } else {

		ib.setImageDrawable(getResources().getDrawable(
			R.drawable.no_starred));
	    }

	    ib.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {

		    items[position].setStarred(!items[position].isStarred());
		    DockController.getInstance().changeStarred(
			    items[position].getIdDock());
		    // notifyDataSetChanged();
		    ListController.notifyChanges();
		}
	    });

	    label.setText(items[position].getIdDock() + " - "
		    + items[position].getAddress());
	    free.setText("Free slots: "
		    + Integer.toString(items[position].getFreeSlots()));
	    available.setText("Available bikes: "
		    + Integer.toString(items[position].getAvailableBikes()));

	    return (row);
	}
    }
}
